<template>
  <div class="login-container">
    <div>
      <img src="https://quickapp-novel-image.oss-cn-shenzhen.aliyuncs.com/backend/static/logo/bimowenxue.png" class="logo">
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <h3 class="title">快应用平台登录</h3>

      <el-form-item prop="passport">
        <el-input
          v-model.trim="loginForm.passport"
          placeholder="用户账号"
          name="passport"
          type="text"
          auto-complete="on"
        >
          <template slot="prefix">
            <svg-icon icon-class="user" />
          </template>
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model.trim="loginForm.password"
          type="password"
          placeholder="用户密码"
          :show-password="true"
          name="password"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        >
          <template slot="prefix">
            <svg-icon icon-class="password" />
          </template>
        </el-input>
      </el-form-item>

      <el-button :loading="loading" class="btn-login" @click.native.prevent="handleLogin">
        登录
      </el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const validatePassport = (rule, value, callback) => {
      if (value.trim() === '') {
        callback(new Error('请输入您的用户账户'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.trim() === '') {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        passport: '',
        password: ''
      },
      verifyCode: '',
      loginRules: {
        passport: [{ required: true, trigger: 'blur', validator: validatePassport }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: '',
      isLogin: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const { passport } = this.loginForm
          const { password } = this.loginForm
          this.$store.dispatch('user/login', { passport, password }).then(res => {
            this.$router.push({ path: this.redirect || '/' })
          }).catch(err => {
            this.$message.error(err.msg || err.message)
          }).finally(() => {
            this.loading = false
          })
        } else {
          console.log('登录失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url('./img/backdrop-img.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 600px;
    height: 400px;
    padding: 50px 100px;
    float: right;
    margin: 6% 13% 0 0;
    overflow: hidden;
    background-color: #fff;
    border-radius:12px;
  }

  & .title {
    color: #0F132D;
    margin: 0px auto 40px auto;
    text-align: center;
    font-size:34px;
  }

  & .logo {
    width: 208px;
    margin: 60px 0 0 128px;
  }

  & .btn-login {
    font-size: 20px;
    height: 48px;
    width: 100%;
    margin-top: 11px;
    background-color: #00BC7E;
    color: #fff;
  }

  & .el-input__inner {
    height: 47px;
    line-height: 47px;
    color: #818286;
    caret-color: #818286;
    -webkit-tap-highlight-color: transparent;

    &:-webkit-autofill {
      box-shadow: 0 0 0px 1000px #fff inset !important;
      -webkit-text-fill-color: #818286 !important;
    }
  }

  & .svg-icon {
    vertical-align: -0.50em;
  }

  & .el-form-item {
    margin-bottom: 30px;
  }
}

@media screen and (max-width: 750px) {
  .login-container {
    -webkit-tap-highlight-color: transparent;

    .login-form {
      width: 100%;
      height: auto;
      padding: 20px 20px 40px 20px;
      margin: 0;
      border-radius: 6px;
    }

    & .title {
      margin-bottom: 20px;
      text-align: center;
      font-size: 26px;
    }

    & .logo {
      width: 208px;
      height: 98px;
      margin: 60px 0 0 128px;
    }

    & .btn-login {
      margin-top: 0;
    }
  }
}
</style>
